package com.fingard.dsp.bank.directbank.abc02;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.File;

public class ABC02RealTime extends ABC02Base implements IRealTime {
    @Override
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        if ("910803".equals(p_req9108.reqHeader.commandCode)) {
            tmpRetDetail = d_9108_CFRA14(p_req9108);
        } else if ("910804".equals(p_req9108.reqHeader.commandCode)) {
            tmpRetDetail = d_9108_CFRTE6(p_req9108);
        } else {
            tmpRetDetail = d_9108_CFRB18(p_req9108);
        }
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    private RetAtsSglDetail d_9108_CFRB18(ReqSingle p_req9108) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        try {
            ABC02Header bankHeader = new ABC02Header();
            bankHeader.CCTransCode = "CFRB18";
            bankHeader.ReqSeqNo = p_req9108.singleDetail.askNumber;

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Cmp");
            boolean isPrivateArea = getBankConfig().getValueAsBool("isPrivateArea");//是否启用广东省农行区域配置
            if (isPrivateArea) {
                //启用
                String ownArea = p_req9108.singleDetail.recAct.substring(0, 2);
                xmlWriter.writeElementString("DbProv", ownArea);
            } else {
                //不启用
                xmlWriter.writeElementString("DbProv", p_req9108.singleDetail.recArea);
            }
            xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req9108.singleDetail.recAct));//收款方账号
            xmlWriter.writeElementString("DbCur", convertCurToBank(p_req9108.singleDetail.recCur));
            xmlWriter.writeElementString("DbLogAccNo", "");//收款方账簿号
//            xmlWriter.writeElementString("SumNum", p_req9108.singleDetail.payAmount);//2019-11-13注释
            xmlWriter.writeElementString("CrAccNo", p_req9108.singleDetail.payAct);//付款方账号

            xmlWriter.writeElementString("CrProv", "2".equals(p_req9108.singleDetail.isPrivate) ? p_req9108.singleDetail.payArea : "");//付款方省市代码

            xmlWriter.writeElementString("CrCur", convertCurToBank(p_req9108.singleDetail.recCur));//付款方币种
            xmlWriter.writeEndElement();
            XElement tmpXElement = new XElement("Corp", "");
            tmpXElement.addChild(new XElement("DbAccName", p_req9108.singleDetail.recName));
            tmpXElement.addChild(new XElement("CrAccName", p_req9108.singleDetail.payName));
            String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_req9108.singleDetail.purpose);
            tmpXElement.addChild(new XElement("NVoucherType", NVoucherType));
            tmpXElement.addChild(new XElement("NFAccNo", getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct)));
            tmpXElement.addChild(new XElement("Postscript", p_req9108.singleDetail.purpose));
            tmpXElement.addChild(new XElement("SpAccInd", "0"));
            tmpXElement.addChild(new XElement("RecInd", "0"));
            nodeAnalysis(p_req9108.singleDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeElementString("Amt", p_req9108.singleDetail.payAmount);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");
                    retDetail.respCode = "0";
                    retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                    retDetail.transState = tmpRespSource + "+" + tmpRespCode;
                    retDetail.payInfoCode = tmpRespCode;
                    retDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetInfo, tmpRetRxtInfo);
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            retDetail.respInfo = ex.getMessage();
            retDetail.respCode = "-1";
            ex.printStackTrace();
        }
        return retDetail;
    }

    private RetAtsSglDetail d_9108_CFRTD2(ReqSingle p_req9108) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        try {
            ABC02Header bankHeader = new ABC02Header();
            bankHeader.CCTransCode = "CFRTD2";
            bankHeader.ReqSeqNo = p_req9108.singleDetail.askNumber;

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbAccNo", p_req9108.singleDetail.recAct);//收款方账号
            xmlWriter.writeElementString("DbProv", p_req9108.singleDetail.recArea);//收款方省市代码
            xmlWriter.writeElementString("DbCur", convertCurToBank(p_req9108.singleDetail.recCur));
            xmlWriter.writeElementString("CrAccNo", p_req9108.singleDetail.payAct);//付款方账号
            xmlWriter.writeElementString("CrProv", p_req9108.singleDetail.payArea);//付款方省市代码
            xmlWriter.writeElementString("CrCur", convertCurToBank(p_req9108.singleDetail.recCur));//付款方币种
            xmlWriter.writeElementString("CheckNameFlag", "0");
            xmlWriter.writeElementString("ConFlag", "1");
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("PayInt", "1");
            xmlWriter.writeElementString("AgtDbInd", "0");
            String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_req9108.singleDetail.purpose);
            xmlWriter.writeElementString("NVoucherType", NVoucherType);
            xmlWriter.writeElementString("CshDraFlag", "0");
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("Amt", p_req9108.singleDetail.payAmount);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");

                    retDetail.respCode = "0";
                    retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                    retDetail.transState = tmpRespSource + "+" + tmpRespCode;
                    retDetail.payInfoCode = tmpRespCode;
                    retDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetInfo, tmpRetRxtInfo);
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            retDetail.respInfo = ex.getMessage();
            retDetail.respCode = "-1";
            ex.printStackTrace();
        }
        return retDetail;
    }

    private RetAtsSglDetail d_9108_CFRTE6(ReqSingle p_req9108) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req9108);
        retDetail.reqSeqID = p_req9108.singleDetail.askNumber;

        boolean isContinue = false;
        ABC02Header bankHeader = new ABC02Header();

        String localFilePath = getTempFileDirectory() + File.separator + p_req9108.singleDetail.payDate + ".txt";
        String voucherBat = FileHelper.readFileText(localFilePath, getCharset());
        if (StringHelper.isNullOrEmpty(voucherBat)) {
            try {
                //申请批次号(一天一次)
                bankHeader.CCTransCode = "CMLT45";
                bankHeader.ReqSeqNo = p_req9108.singleDetail.askNumber;
                bankHeader.ReqDateTime = p_req9108.singleDetail.getPayDateTime();
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Cmp");
                if (StringHelper.hasAnyChar(p_req9108.singleDetail.recArea)) {
                    xmlWriter.writeElementString("DbProv", p_req9108.singleDetail.recArea);
                } else {
                    xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProv" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct));
                }
                xmlWriter.writeElementString("DbAccNo", p_req9108.singleDetail.recAct);
                xmlWriter.writeElementString("DbCur", convertCurToBank(p_req9108.singleDetail.recCur));
                xmlWriter.writeElementString("DbLogAccNo", "");
                xmlWriter.writeEndElement();
                xmlWriter.writeStartElement("Corp");
                String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_req9108.singleDetail.useType);
                xmlWriter.writeElementString("NVoucherType", NVoucherType);
                String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct);
                xmlWriter.writeElementString("NFAccNo", NFAccNo);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        while (tmpBkReader.read("/ap/Corp")) {
                            voucherBat = tmpBkReader.readedXElement.getSingleTextNotNull("VoucherBat");
                            if (StringHelper.hasAnyChar(voucherBat)) {
                                localFilePath = createTempFileDirectory() + File.separator + p_req9108.singleDetail.payDate + ".txt";
                                FileHelper.writeFile(localFilePath, voucherBat, getCharset());
                                isContinue = true;
                            } else {
                                isContinue = false;
                                retDetail.respCode = "-1";
                                retDetail.respInfo = tmpSbMsg.toString();
                            }
                        }
                    } else {
                        isContinue = false;
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    isContinue = false;
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpStrRet[1];
                    retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                    retDetail.transState = PaymentState.FAIL_Payment;
                    retDetail.payInfoCode = tmpStrRet[0];
                } else {
                    isContinue = false;
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                isContinue = false;
                retDetail.respInfo = ex.getMessage();
                retDetail.respCode = "-1";
                ex.printStackTrace();
            }
        } else {
            isContinue = true;
        }
        //单笔代收接口
        if (isContinue) {
            try {
                bankHeader.CCTransCode = "CFRTE6";
                bankHeader.ReqSeqNo = p_req9108.singleDetail.askNumber;
                bankHeader.ReqDateTime = p_req9108.singleDetail.getPayDateTime();
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Cmp");
                if (StringHelper.hasAnyChar(p_req9108.singleDetail.recArea)) {
                    xmlWriter.writeElementString("DbProv", p_req9108.singleDetail.recArea);
                } else {
                    xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProv" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct));
                }
                xmlWriter.writeElementString("DbAccNo", p_req9108.singleDetail.recAct);
                xmlWriter.writeElementString("DbCur", convertCurToBank(p_req9108.singleDetail.recCur));
                xmlWriter.writeElementString("CrProv", p_req9108.singleDetail.payArea);
                xmlWriter.writeElementString("CrAccNo", getAbcAccNo(p_req9108.singleDetail.payAct));
                xmlWriter.writeElementString("CrCur", convertCurToBank(p_req9108.singleDetail.recCur));
                xmlWriter.writeElementString("CrLogAccNo", "");
                xmlWriter.writeEndElement();
                xmlWriter.writeStartElement("Corp");
                String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_req9108.singleDetail.useType);
                xmlWriter.writeElementString("NVoucherType", NVoucherType);
                String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct);
                xmlWriter.writeElementString("NFAccNo", NFAccNo);
                xmlWriter.writeElementString("CustomNo", p_req9108.singleDetail.askNumber);
                xmlWriter.writeElementString("CrAccName", p_req9108.singleDetail.payName);
                xmlWriter.writeElementString("ContPhone", p_req9108.singleDetail.cellPhone);
                xmlWriter.writeElementString("SpAccInd", "0");
                xmlWriter.writeElementString("RecInd", "0");
                xmlWriter.writeElementString("PostScript", "");
                xmlWriter.writeElementString("VoucherBat", voucherBat);
                xmlWriter.writeEndElement();
                xmlWriter.writeElementString("Amt", p_req9108.singleDetail.payAmount);
                xmlWriter.writeStartElement("Acc");
                xmlWriter.writeElementString("CertType", convertCertTypeToBank(p_req9108.singleDetail.certType));
                xmlWriter.writeElementString("CertNo", p_req9108.singleDetail.certNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                        String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                        String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                        String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");

                        retDetail.respCode = "0";
                        retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                        retDetail.transState = tmpRespSource + "+" + tmpRespCode;
                        retDetail.payInfoCode = tmpRespCode;
                        retDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetInfo, tmpRetRxtInfo);
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpStrRet[1];
                    retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                    retDetail.transState = PaymentState.FAIL_Payment;
                    retDetail.payInfoCode = tmpStrRet[0];
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                retDetail.respInfo = ex.getMessage();
                retDetail.respCode = "-1";
                ex.printStackTrace();
            }
        }
        return retDetail;
    }

    private RetAtsSglDetail d_9108_CFRA14(ReqSingle p_req9108) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req9108);
        retDetail.reqSeqID = p_req9108.singleDetail.askNumber;

        boolean isContinue = false;
        ABC02Header bankHeader = new ABC02Header();

        String localFilePath = getTempFileDirectory() + File.separator + p_req9108.singleDetail.payDate + ".txt";
        String voucherBat = FileHelper.readFileText(localFilePath, getCharset());
        if (StringHelper.isNullOrEmpty(voucherBat)) {
            try {
                //申请批次号(一天一次)
                bankHeader.CCTransCode = "CMLT45";
                bankHeader.ReqSeqNo = p_req9108.singleDetail.askNumber;
                bankHeader.ReqDateTime = p_req9108.singleDetail.getPayDateTime();
                XmlTextWriter xmlWriter = new XmlTextWriter();

                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Cmp");
                if (StringHelper.hasAnyChar(p_req9108.singleDetail.recArea)) {
                    xmlWriter.writeElementString("DbProv", p_req9108.singleDetail.recArea);
                } else {
                    xmlWriter.writeElementString("DbProv", getBankConfig().getMapValueByKey("dbProv" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct));
                }
                xmlWriter.writeElementString("DbAccNo", p_req9108.singleDetail.recAct);
                xmlWriter.writeElementString("DbCur", convertCurToBank(p_req9108.singleDetail.recCur));
                xmlWriter.writeElementString("DbLogAccNo", "");
                xmlWriter.writeEndElement();
                xmlWriter.writeStartElement("Corp");
                String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_req9108.singleDetail.purpose);
                xmlWriter.writeElementString("NVoucherType", NVoucherType);
                String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct);
                xmlWriter.writeElementString("NFAccNo", NFAccNo);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                /*String[] tmpStrRet = {"","<ap>\n" +
                        "  <Channel>\n" +
                        "    <AccDate>20190924</AccDate>\n" +
                        "    <JrnNo>193990434</JrnNo>\n" +
                        "    <VchNo>0</VchNo>\n" +
                        "    <RespInfo></RespInfo>\n" +
                        "    <RxtInfo></RxtInfo>\n" +
                        "    <AbisRespCode>0000</AbisRespCode>\n" +
                        "  </Channel>\n" +
                        "  <RespCode>0000</RespCode>\n" +
                        "  <Corp>\n" +
                        "    <VoucherBat>65</VoucherBat>\n" +
                        "  </Corp>\n" +
                        "  <CCTransCode>CMLT45</CCTransCode>\n" +
                        "  <RespSource>0</RespSource>\n" +
                        "  <RespInfo>交易成功</RespInfo>\n" +
                        "  <RxtInfo></RxtInfo>\n" +
                        "  <RespDate>20200401</RespDate>\n" +
                        "  <RespTime>181615</RespTime>\n" +
                        "  <FileFlag>0</FileFlag>\n" +
                        "  <Cme>\n" +
                        "    <RecordNum>0</RecordNum>\n" +
                        "    <FieldNum>0</FieldNum>\n" +
                        "  </Cme>\n" +
                        "  <RespSeqNo></RespSeqNo>\n" +
                        "  <Cmp>\n" +
                        "    <BatchFileName></BatchFileName>\n" +
                        "    <RespPrvData></RespPrvData>\n" +
                        "  </Cmp>\n" +
                        "</ap>"};*/
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        while (tmpBkReader.read("/ap/Corp")) {
                            voucherBat = tmpBkReader.readedXElement.getSingleTextNotNull("VoucherBat");
                            if (StringHelper.hasAnyChar(voucherBat)) {
                                localFilePath = createTempFileDirectory() + File.separator + p_req9108.singleDetail.payDate + ".txt";
                                FileHelper.writeFile(localFilePath, voucherBat, getCharset());
                                isContinue = true;
                            } else {
                                isContinue = false;
                                retDetail.respCode = "-1";
                                retDetail.respInfo = tmpSbMsg.toString();
                            }
                        }
                    } else {
                        isContinue = false;
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    isContinue = false;
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpStrRet[1];
                    retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                    retDetail.transState = PaymentState.FAIL_Payment;
                    retDetail.payInfoCode = tmpStrRet[0];
                } else {
                    isContinue = false;
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                isContinue = false;
                retDetail.respInfo = ex.getMessage();
                retDetail.respCode = "-1";
                ex.printStackTrace();
            }
        } else {
            isContinue = true;
        }
        //单笔代收接口
        if (isContinue) {
            try {

                bankHeader.CCTransCode = "CFRA14";
                bankHeader.ReqSeqNo = p_req9108.singleDetail.askNumber;
                bankHeader.ReqDateTime = p_req9108.singleDetail.getPayDateTime();
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("Cmp");
                if (StringHelper.hasAnyChar(p_req9108.singleDetail.recArea)) {
                    xmlWriter.writeElementString("CrProv", p_req9108.singleDetail.recArea);
                } else {
                    xmlWriter.writeElementString("CrProv", getBankConfig().getMapValueByKey("dbProv" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct));
                }
                xmlWriter.writeElementString("CrAccNo", getAbcAccNo(p_req9108.singleDetail.recAct));
                xmlWriter.writeElementString("CrCur", convertCurToBank(p_req9108.singleDetail.recCur));
                xmlWriter.writeElementString("CrLogAccNo", "");
                xmlWriter.writeElementString("ConFlag", "1");
                xmlWriter.writeEndElement();
                xmlWriter.writeStartElement("Corp");
                String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_req9108.singleDetail.useType);
                xmlWriter.writeElementString("NVoucherType", NVoucherType);
                String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_req9108.singleDetail.recAct);
                xmlWriter.writeElementString("NFAccNo", NFAccNo);
                xmlWriter.writeElementString("CustomNo", p_req9108.singleDetail.askNumber);
                xmlWriter.writeElementString("CrAccName", p_req9108.singleDetail.payName);
                xmlWriter.writeElementString("ContPhone", p_req9108.singleDetail.cellPhone);
                xmlWriter.writeElementString("SpAccInd", "0");
                xmlWriter.writeElementString("RecInd", "0");
                xmlWriter.writeElementString("PostScript", "");
                xmlWriter.writeElementString("VoucherBat", voucherBat);
                xmlWriter.writeEndElement();
                xmlWriter.writeElementString("Amt", p_req9108.singleDetail.payAmount);
                xmlWriter.writeStartElement("Acc");
                xmlWriter.writeElementString("CertType", convertCertTypeToBank(p_req9108.singleDetail.certType));
                xmlWriter.writeElementString("CertNo", p_req9108.singleDetail.certNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                        String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                        String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                        String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");

                        retDetail.respCode = "0";
                        retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                        retDetail.transState = tmpRespSource + "+" + tmpRespCode;
                        retDetail.payInfoCode = tmpRespCode;
                        retDetail.respInfo = StringHelper.joinNotEmpty("+", tmpRetInfo, tmpRetRxtInfo);
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpStrRet[1];
                    retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                    retDetail.transState = PaymentState.FAIL_Payment;
                    retDetail.payInfoCode = tmpStrRet[0];
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpStrRet[1];
                }
            } catch (Exception ex) {
                retDetail.respInfo = ex.getMessage();
                retDetail.respCode = "-1";
                ex.printStackTrace();
            }
        }
        return retDetail;
    }

    @Override
    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);

        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        tmpRetDetail.respCode = "";
        try {
            ABC02Header bankHeader = new ABC02Header();
            bankHeader.CCTransCode = "CQRT71";
            bankHeader.ReqSeqNo = p_req9144.reqHeader.transSeqID;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter, bankHeader);//写入通用的头部节点
            xmlWriter.writeStartElement("Cmp");
            boolean isPrivateArea = getBankConfig().getValueAsBool("isPrivateArea");//是否启用广东省农行区域配置
            if (isPrivateArea) {
                //启用
                String ownArea = p_req9144.recAct.substring(0, 2);
                xmlWriter.writeElementString("DbProv", ownArea);
            } else {
                //不启用
                xmlWriter.writeElementString("DbProv", p_req9144.recArea);
            }
            xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req9144.recAct));
            xmlWriter.writeElementString("DbCur", convertCurToBank(p_req9144.recCur));
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Cme");
            xmlWriter.writeElementString("SerialNo", p_req9144.askNumber);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespPrvData = tmpBkReader.getFirstText("/ap/Cmp/RespPrvData");
                    if (tmpRetCode != null && tmpRetCode.equals("0000")) {
                        if (tmpBkReader.read("/ap/Cmp")) {
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = tmpRespPrvData;
                            tmpRetDetail.reqSeqID = p_req9144.askNumber;
                            tmpRetDetail.transState = tmpRetCode + "+" + tmpBkReader.readedXElement.getSingleText("TransSta");
                            tmpRetDetail.payInfoCode = tmpRetDetail.transState;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        if (!StringHelper.isNullOrEmpty(tmpRetRxtInfo)) {
                            tmpRetDetail.respInfo = tmpRetRxtInfo;
                        } else if (!StringHelper.isNullOrEmpty(tmpRetInfo)) {
                            tmpRetDetail.respInfo = tmpRetInfo;
                        } else {
                            tmpRetDetail.respInfo = tmpRespPrvData;
                        }
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }

        } catch (Exception ex) {
            tmpRetDetail.respInfo = ex.getMessage();
            tmpRetDetail.respCode = "-1";
            ex.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}
